package com.xiaodu.dynamicdatasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.xiaodu.dynamicdatasource.config.properties.GlobalDruidDatasourceProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author: dutianjie
 * @date: 2021/4/22
 * @version: v1.0.0
 * @description: 描述
 **/
@Configuration
@ConditionalOnClass(DataSource.class)
@EnableConfigurationProperties(GlobalDruidDatasourceProperties.class)
public class DataConfig {

    private GlobalDruidDatasourceProperties globalDruidDatasourceProperties;

    private Map<Object, Object> dataSourceMap = new HashMap<>(8);

    public DataConfig(GlobalDruidDatasourceProperties globalDruidDatasourceProperties) {
        this.globalDruidDatasourceProperties = globalDruidDatasourceProperties;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    @Primary
    public DataSource mysqlDataSource() {
        DruidDataSource mysqlDruidDataSource = DruidDataSourceBuilder.create().build();
        configurationDataSource(mysqlDruidDataSource);
        return mysqlDruidDataSource;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.pg")
    public DataSource pgDataSource() {
        DruidDataSource pgDruidDataSource = DruidDataSourceBuilder.create().build();
        configurationDataSource(pgDruidDataSource);
        return pgDruidDataSource;
    }

    @Bean
    public DynamicDataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dataSourceMap.put("mysqlDataSource", mysqlDataSource());
        dataSourceMap.put("pgDataSource", pgDataSource());
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        DataSourceContextHolder.datasourceKeySet.addAll(dataSourceMap.keySet().stream().map(Object::toString).collect(toList()));
        dynamicDataSource.setDefaultTargetDataSource(mysqlDataSource());
        return dynamicDataSource;
    }


    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }


    private void configurationDataSource(DruidDataSource druidDataSource) {
        druidDataSource.setMaxActive(globalDruidDatasourceProperties.getMaxActive());
        druidDataSource.setMinIdle(globalDruidDatasourceProperties.getMinIdle());
        druidDataSource.setInitialSize(globalDruidDatasourceProperties.getInitialSize());
        druidDataSource.setMaxWait(globalDruidDatasourceProperties.getMaxWait());
    }

}
